package com.example.a2209_shop.register

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.base.entity.UserEntityResult
import com.example.base.mvvm.BaseViewModel
import com.google.gson.Gson
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import kotlin.math.log

class RegisterViewModel:BaseViewModel() {
    val registerSuccess = MutableLiveData<UserEntityResult>()
    val registerFail = MutableLiveData<String>()

    fun register(username:String,password:String,repassword:String){
        Log.d("sss", "register: 1223456")
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map.put("username",username)
                map.put("password",password)
                map.put("repassword",repassword)
                //map.put("code",123)
                val json = Gson().toJson(map)
                Log.d("ssss", "register: yuiooo")
                val body = RequestBody.create("application/json;charset=utf-8".toMediaTypeOrNull(),json)
                registerSuccess.value = apiServer.register(body)


            }catch (e:Exception){
                Log.d("ssss", "registerwer56ty: ${e.message}")
                registerFail.value = e.message
            }
        }
    }
}